using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class SettingAccessor : ISettingRepository
    {
        static bool Cache = true;
        static string Key = "DalatCooking_Setting";
       

        public override bool Insert(Setting entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@SettingId", "Setting_Insert", entity.SettingId, entity.Name, entity.Value);
            DataCache.RemoveCache(Key);
            return int.Parse(rs.ToString()) > 0;
        }
        public override bool Update(Setting entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Setting_Update", entity.SettingId, entity.Name, entity.Value);
            DataCache.RemoveCache(Key);
            return rs > 0;
        }
        public override bool Delete(int id)
        {
            DataCache.RemoveCache(Key);
            return IDataProvider.Instance.ExecuteNonQuery("Setting_Delete", id) > 0;
        }
        public override bool Delete(Setting entity)
        {
            return Delete(entity.SettingId);
        }
        public override List<Setting> FindAll()
        {
            if (Cache)
            {
                List<Setting> list = DataCache.GetCache(Key) as List<Setting>;
                if (list == null || list.Count == 0)
                {
                    list = CBO.FillCollection<Setting>(IDataProvider.Instance.ExecuteReader("Setting_FindAll"));
                    list.TrimExcess();
                    DataCache.SetCache(Key, list);
                }
                return list;
            }
            
            return CBO.FillCollection<Setting>(IDataProvider.Instance.ExecuteReader("Setting_FindAll"));
        }
        public override Setting FindById(int id)
        {
            return CBO.FillObject<Setting>(IDataProvider.Instance.ExecuteReader("Setting_FindById", id));
        }
        public override Setting Single(int id)
        {
            return FindById(id);
        }
        public override Setting FindByName(string name)
        {
            return CBO.FillObject<Setting>(IDataProvider.Instance.ExecuteReader("Setting_FindByName", name));
        }
        public override List<Setting> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Setting_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Setting>(reader);
        }
        public override List<Setting> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
            var query = from c in FindAll()
                        select c;
            totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Setting_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Setting_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Setting_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Setting_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Setting_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Setting_RestoreAll");
            return rs > 0;
        }
    }
}
